package com.zstu.mall.web.user;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.zstu.mall.entity.Order;
import com.zstu.mall.entity.Product;
import com.zstu.mall.entity.UserOrder;
import com.zstu.mall.entity.pojo.ResultBean;
import com.zstu.mall.service.OrderService;
import com.zstu.mall.service.ProductService;
import com.zstu.mall.service.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserOrderService userOrderService;

    @Autowired
    private ProductService productService;

    /**
     * 打开订单列表页面
     *
     * @return
     */
    @RequestMapping("/toList.html")
    public String toOrderList() {
        return "mall/order/list";
    }

    /**
     * 打开支付页面
     *
     * @return
     */
    @RequestMapping("/toPay.html")
    public String toOrderPayment(String id, Map<String, Object> map) {
        Order order = orderService.get(id);
        map.put("order", order);
        List<UserOrder> userOrders = userOrderService.findByOrderId(id);
        map.put("userOrders", userOrders);
        return "mall/order/payPage";
    }

    /**
     * 打开确认页面
     *
     * @return
     */
    @RequestMapping("/toConfirm.html")
    public String toConfirm(String id, Map<String, Object> map) {
        //订单信息
        Order order = orderService.get(id);
        map.put("order", order);
        //订单对应购物车的信息
        List<UserOrder> userOrders = userOrderService.findByOrderId(id);//购物车状态通过id = shop_cart_ + user_id 来识别
        map.put("userOrders", userOrders);
        return "mall/order/confirm";
    }

    /**
     * 查询用户订单列表(包含查看订单详情)
     *
     * @param request
     * @return
     */
    @RequestMapping("/list.do")
    @ResponseBody
    public ResultBean<List<Order>> listData(HttpServletRequest request) {
        List<Order> orders = orderService.findUserOrder(request);
        //为订单列表添加具体订单信息
        for(Order order : orders){
            List<UserOrder> orderItems = orderService.findItems(order.getId());
            order.setUserOrders(orderItems);
        }
        return new ResultBean<>(orders);
    }

    /**
     * 查询订单详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/getDetail.do")
    @ResponseBody
    public ResultBean<List<UserOrder>> getDetail(String orderId) {
        List<UserOrder> orderItems = orderService.findItems(orderId);
        return new ResultBean<>(orderItems);
    }

    /**
     * 提交订单
     *
     * @param ids
     * @param request
     * @param response
     */
    @RequestMapping(value = "/submit.do", method = RequestMethod.POST)
    public void submit(@RequestBody List<String> ids,
                       HttpServletRequest request,
                       HttpServletResponse response) throws Exception {
        orderService.submit(ids, request, response);
    }

    /**
     * 确认订单
     * @param id
     * @param name
     * @param phone
     * @param addr
     * @param request
     * @param response
     */
    @RequestMapping("/confirm.do")
    @ResponseBody
    public ResultBean<Boolean> confirm(String id,
                       String name,
                       String phone,
                       String addr,
                       HttpServletRequest request,
                       HttpServletResponse response) throws Exception {
        orderService.confirm(id, name, phone, addr, request, response);
        return new ResultBean<>(true);
    }

    /**
     * 支付
     * @param orderId
     * @param name
     * @param payPassword
     */
    @RequestMapping("/pay.do")
    @ResponseBody
    public ResultBean<Boolean> pay(String orderId, String name, String payPassword, HttpServletResponse response) throws IOException {
        orderService.pay(orderId, name, payPassword);
        return new ResultBean<>(true);
    }

    /**
     * 确认收货
     * @param orderId
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/receive.do")
    @ResponseBody
    public ResultBean<Boolean> receive(String orderId, HttpServletResponse response) throws IOException {
        orderService.receive(orderId);
        return new ResultBean<>(true);
    }
}
